<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Zhuzhichao\IpLocationZh\Ip;

class IptoRegion {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        
        $ip = $request->getClientIps()[0];
        if ($ip !="127.0.0.1" && !$request->session()->has($ip)) {//session里没有ip
            // 注意 $region 的组织形式
            // 数组 
            // [0] => 国家, [1] => 省, [2]=>市, [3]=>区, [4]=>邮编
            $region = Ip::find($ip);
            $request->session()->put('ip', $region);
        }
        return $next($request);
    }
}
